<?php

namespace app\api\controller;

use app\BaseController;
use frame\lib\Db;

class Sign extends Base
{
    public function do()
    {
        $sign = Db::name('sign')->where(['uid' => $this->uid])->find();
        $conf = Db::name('config')->where(['user_id' => $this->user_id])->find()['conf'];
        $conf = json_decode($conf, true);
        $userScore = Db::name('user')->where(['id' => $this->uid])->find()['score'] ?? 0;
        if ($conf['is_sign'] == 1) {
            $score = $this->scoreCmp($conf);
            $totalScore = $userScore + $score;
            if ($sign) {
                if (date('d', strtotime($sign['sign_time'])) == date('d', time())) error('一天只能签到一次哦！');
                else {
                    $total = $sign['total'] + 1;
                    Db::name('user')->where(['id' => $this->uid])->update(['score' => $totalScore]);
                    if (date('d', strtotime($sign['sign_time'])) - 1 != date('d', time())) {
                        Db::name('sign')->where(['uid' => $this->uid , 'user_id' => $this->user_id])->update(['sign_time' => getTime(), 'total' => $total, 'continus_sign' => 1]);
                    } else {
                        Db::name('sign')->where(['uid' => $this->uid , 'user_id' => $this->user_id])->update(['sign_time' => getTime(), 'total' => $total, 'continus_sign' => $sign['continus_sign']++]);
                    }
                }
            } else {
                $data = ['uid' => $this->uid,'user_id' => $this->user_id,  'sign_time' => getTime(), 'total' => 1, 'continus_sign' => 1];
                Db::name('sign')->insert($data);
                Db::name('user')->where(['id' => $this->uid])->update(['score' => $totalScore]);
            }
            success();
        }
        error('已关闭签到了哦 !');
    }
    public function scoreCmp($conf)
    {
        $continus = $sign['continus_sign'] ?? 1;
        if ($continus < 3) {
            $score = $conf['sign_three'];
        } else if ($continus >= 7 && $continus < 15) {
            $score = $conf['sign_seven'];
        } else if ($continus >= 15) {
            $score = $conf['sign_fifteen'];
        }
        return $score;
    }
}
